/**
 * 
 */
package com.ez.supervise.service.impl;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.core.Page;
import com.ez.supervise.dao.GoalDetailDao;
import com.ez.supervise.dao.GoalSelfDao;
import com.ez.supervise.dao.OrgCheckDao;
import com.ez.supervise.model.biz.GoalDetail;
import com.ez.supervise.model.biz.GoalSelf;
import com.ez.supervise.model.biz.OrgCheck;
import com.ez.supervise.service.GoalSelfService;

/**
 * @author searph.wang
 * @email searph.wang@gmail.com
 * @Created at 2009-7-22
 * 
 */
public class GoalSelfServiceImpl implements GoalSelfService {
    private GoalSelfDao goalSelfDao;
    private OrgCheckDao orgCheckDao; 
 
    public void setOrgCheckDao(OrgCheckDao orgCheckDao) {
	this.orgCheckDao = orgCheckDao;
    }

    // public int update(GoalSelf org) {
    // return sqlMapClientTemplate.update("sys.updateGoalSelf", org);
    // }

    public void setGoalSelfDao(GoalSelfDao goalSelfDao) {
	this.goalSelfDao = goalSelfDao;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#delete(java.lang.Long)
     */
    public void delete(Long id) {
	goalSelfDao.delete(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#insert(java.lang.Object)
     */
    public void insert(Object entity) {
	goalSelfDao.insert(entity);

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#update(java.lang.Object)
     */
    public void update(Object entity) {
	goalSelfDao.update(entity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#get(java.lang.Long)
     */
    public Object get(Long id) {
	return (GoalSelf) goalSelfDao.get(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.GoalSelfService#findAll()
     */
    public List findAll() {
	return goalSelfDao.findAll();
    }

    public void findGoalSelfByPage(Page page) {
	page.setRoot(goalSelfDao.findGoalSelfByPage(page));
	page.setTotalProperty(goalSelfDao.findGoalSelfByCount(page));
    }

    public void auditGoalSelf(GoalSelf goalSelf) {
	goalSelfDao.update(goalSelf);
	/* 审核通过要保存到考核设置中 */
	if (GoalSelf.AUDIT_YES.equalsIgnoreCase(goalSelf.getAuditResult())) {
	    OrgCheck orgCheck = new OrgCheck(goalSelf.getOrgId(), goalSelf.getId(), OrgCheck.TYPE_SELF);
	    orgCheck.setCheckStatus(OrgCheck.CHECK_STATUS_NEW);
	    orgCheck.setYear(goalSelf.getYear());
	    orgCheck.setCheckOrgId(goalSelf.getCheckOrgId());
	    int count = orgCheckDao.countExisOrgCheck(orgCheck);
	    if (count == 0) {
		orgCheckDao.insert(orgCheck);
	    }
	} else if (GoalSelf.AUDIT_NO.equalsIgnoreCase(goalSelf.getAuditResult())) {

	}
    }

    /* (non-Javadoc)
     * @see com.ez.supervise.service.GoalSelfService#syncGoalSelf(int, long)
     */
    public void syncGoalSelf(int year, long orgId) {
	//取得所有已审核的指标
	List<GoalSelf> goalSelfList = goalSelfDao.getAllAuditGoalSelf(year, orgId);
	/* 审核通过要保存到考核设置中 */
	for (GoalSelf goalSelf : goalSelfList) {
	    OrgCheck orgCheck = new OrgCheck(goalSelf.getOrgId(), goalSelf.getId(), OrgCheck.TYPE_SELF);
	    orgCheck.setCheckStatus(OrgCheck.CHECK_STATUS_NEW);
	    orgCheck.setYear(goalSelf.getYear());
	    orgCheck.setCheckOrgId(goalSelf.getCheckOrgId());
	    int count = orgCheckDao.countExisOrgCheck(orgCheck);
	    if (count == 0) {
		orgCheckDao.insert(orgCheck);
	    }
	}
    }
}
